/*
 */
package com.ingenic.glass.camera;

import java.io.IOException;
import java.util.List;
import java.io.File;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.Size;
import android.location.Location;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore.Images;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.OrientationEventListener;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.content.ContentValues;
import com.ingenic.glass.camera.gallery.ImageGetter;
import com.ingenic.glass.camera.gallery.BitmapManager;
import com.ingenic.glass.camera.util.Exif;
import com.ingenic.glass.camera.util.StorageSpaceUtil;
import com.ingenic.glass.camera.util.Util;
import com.ingenic.glass.voicerecognizer.api.VoiceRecognizer;
import com.ingenic.glass.voicerecognizer.api.VoiceRecognizerListener;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.ImageColumns;

/** The QuickCapture class which can take pictures quickly. */
public class QuickCapture implements SurfaceHolder.Callback {
    private static final String TAG = "QuickCapture";
    private FrameLayout mPreviewFrameLayout;
    private SurfaceView mPreviewFrame;  // Preview frame area.
    private Context mContext;
    private Parameters mParameters;
    private ComboPreferences mPreferences;
    private SurfaceHolder mSurfaceHolder = null;
    private boolean mOpenCameraFail = false;
    private boolean mCameraDisabled = false;
    private boolean mCameraBusy = false;
    private static final int PREVIEW_STOPPED = 0;
    private static final int IDLE = 1;  // preview is active
    private static final int SNAPSHOT_IN_PROGRESS = 2;
    private int mCameraState = PREVIEW_STOPPED;

    private ContentResolver mContentResolver;
    private LocationManager mLocationManager;
    private OnScreenHint mStorageHint;
    private long mPicturesRemaining;

    private int mCameraId;
    private Camera mCameraDevice;
    private View mRootView;
    private ImageView mAnimationView_1;
    private ImageView mAnimationView_2;
    private TranslateAnimation t1;
    private TranslateAnimation t2;
    private WindowManager mWindowManager;
    private WindowManager.LayoutParams mWindowParams;

    // play camera click sound effect
    private MediaPlayer mPlayer;
    private VoiceRecognizer mVoiceRecognizer = null;
    private static QuickCapture mInstance = null;

    private int windowW ;
    private int windowH;
    private final Handler mHandler = new MainHandler();
    private final CameraErrorCallback mErrorCallback = new CameraErrorCallback();
    private boolean IS_USE_QuickCapture_HALStore = false;
    private boolean QuickCapture_HALStoreJpeg_Flag = true;
    private String mQuickCapture_HALStoreJpeg_fullpath = null;
    private int mPicWaterMark = 0;
    private int mVideoWaterMark = 0;
    public QuickCapture(Context context){
	mContext = context;
	if (CameraAppImpl.USE_TTS) {
		mVoiceRecognizer = new VoiceRecognizer(VoiceRecognizer.REC_TYPE_COMMAND, new VoiceRecognizerListener());
	}
    }

    private class MainHandler extends Handler {
	@Override
	public void handleMessage(Message msg) {
		if (CameraAppImpl.DEBUG)
			Log.d(TAG, "handleMessage in msg.what=" + msg.what);
		switch (msg.what) {
		/* 如果CameraHal存储时，发生错误 */
		case CameraAppImpl.CAMERA_ERROR_QUICKCAPTURE_HAL_STORE:
			QuickCapture_HALStoreJpeg_Flag = false;
			break;
		case CameraAppImpl.CAMERA_ERROR_PREVIEW:{
			showToast(R.string.capture_failed);
			Log.e(TAG, "Error message: CAMERA_ERROR_PREVIEW");
			if (mVoiceRecognizer != null)
				mVoiceRecognizer.playTTS(mContext.getString(R.string.tts_take_picture_failed));
			finish();
			break;
		}
		default:
			if (CameraAppImpl.DEBUG)
				Log.d(TAG, "Unhandled message: " + msg.what);
			if (mVoiceRecognizer != null)
				mVoiceRecognizer.playTTS(mContext.getString(R.string.tts_take_picture_failed));
			stopPreview();
			finish();
			break;
		}
	}
    }

    private final class JpegPictureCallback implements PictureCallback {
        Location mLocation;

        public JpegPictureCallback(Location loc) {
            mLocation = loc;
        }

        public void onPictureTaken(final byte [] jpegData, final android.hardware.Camera camera) {
            if (CameraAppImpl.DEBUG) Log.d(TAG, "------onPictureTaken in");

	    if (CameraAppImpl.DEBUG) {
		    Log.e(TAG, "IS_USE_QuickCapture_HALStore = " + IS_USE_QuickCapture_HALStore);
		    Log.e(TAG, "QuickCapture_HALStoreJpeg_Flag = " + QuickCapture_HALStoreJpeg_Flag);
	    }
	    /**
	     * IS_USE_QuickCapture_HALStore   : 是否使用CameraHal存储
	     * QuickCapture_HALStoreJpeg_Flag : CameraHal存储是否成功
	     */
	    if (IS_USE_QuickCapture_HALStore && QuickCapture_HALStoreJpeg_Flag) {
		    /* 使用CameraHal存储并且存储成功，停止预览，结束快拍 */
		    if (CameraAppImpl.DEBUG)
			    Log.d(TAG, "Camera Hal Store Jpeg success.");
		    /* 如果你想拍完照后，照片停留，延迟结束finish即可 */
		    // try {	
		    // 	    Thread.currentThread().sleep(20000);//阻断20秒
		    // } catch (InterruptedException e) {
		    // 	    e.printStackTrace();
		    // }
		    stopPreview();
		    notifyMediaScanner();
		    finish();
	    } else if (IS_USE_QuickCapture_HALStore && (!QuickCapture_HALStoreJpeg_Flag)) {
		    /* 使用CameraHal存储但是存储失败，此时删除CameraHal存储时应用生成的文件，并且应用重新存储文件；
		       然后停止预览，结束快拍 */
		    new File(mQuickCapture_HALStoreJpeg_fullpath).delete();
		    stopPreview();
		    storeImage(jpegData, mLocation);
		    finish();
	    } else {
		    /* 否则，重新存储图片，并停止预览，结束快拍 */
		    stopPreview();
		    storeImage(jpegData, mLocation);
		    finish();
	    }
	}
    }

    private void storeImage(final byte[] data, Location loc) {
        long dateTaken = System.currentTimeMillis();
        String title = Util.createJpegName(dateTaken);
        int orientation = Exif.getOrientation(data);
        Size s = mParameters.getPictureSize();
        Uri uri = Storage.addImage(mContentResolver, title, dateTaken, loc, 
				   orientation, data, s.width, s.height);
        if (!CameraAppImpl.NO_SCREEN_DEVICE)
		BitmapManager.instance().getThumbnail(mContentResolver, ContentUris.parseId(uri),
						      Images.Thumbnails.MINI_KIND, null, false);
    }

    private boolean takePicture() {
	if (CameraAppImpl.DEBUG) Log.d(TAG, "takePicture in mCameraState = "+mCameraState);
        // If we are already in the middle of taking a snapshot then ignore.
        if (mCameraState == SNAPSHOT_IN_PROGRESS || mCameraDevice == null) {
            return false;
        }
        // Set rotation and gps data.
        Util.setRotationParameter(mParameters, mCameraId, OrientationEventListener.ORIENTATION_UNKNOWN);
        Location loc = mLocationManager.getCurrentLocation();
        Util.setGpsParameters(mParameters, loc);
	if (CameraAppImpl.DEBUG) Log.d(TAG,"call takePicture------");
        mCameraDevice.takePicture(null, null, null, new JpegPictureCallback(loc));
        mCameraState = SNAPSHOT_IN_PROGRESS;
	return true;
    }

    private void getPreferredCameraId() {
        mPreferences = new ComboPreferences(mContext);
        CameraSettings.upgradeGlobalPreferences(mPreferences.getGlobal());
        mCameraId = CameraSettings.readPreferredCameraId(mPreferences);
    }

    private void openCamera() {
	try {
	    mCameraDevice = Util.openCamera(mContext, mCameraId);
            if (CameraAppImpl.DEBUG) Log.d(TAG,"mCameraDevice="+mCameraDevice);
	    if (mCameraDevice == null)
		mOpenCameraFail = true;
	} catch (CameraHardwareException e) {
	    mOpenCameraFail = true;
	    Log.e(TAG,"openCamera failed");
	} catch (CameraDisabledException e) {
	    mCameraDisabled = true;
	    Log.e(TAG,"camera disabled");
	} catch(Exception e){
	    mCameraBusy = true;
	    Log.e(TAG,"camera busy");
	}
    }

    public void start() {
	if (CameraAppImpl.DEBUG) Log.d(TAG,"start in");
	try{
	    int currentBatteryVoltage = Settings.System.getInt(mContext.getContentResolver(),
							       "batteryVoltage");
	    if (CameraAppImpl.DEBUG) Log.d(TAG,"currentBatteryVoltage = "+currentBatteryVoltage);
	    if (currentBatteryVoltage <= CameraAppImpl.LOWEST_BATTERY_VOLTAGE){
		if (mVoiceRecognizer != null)
			mVoiceRecognizer.playTTS(mContext.getString(R.string.tts_take_picture_lowpower));
		else
			showToast(R.string.battery_low_hint);
		finish();
		return;
	    }
	}catch(SettingNotFoundException  e){
	    e.printStackTrace();
	}

	if(checkStorage() == false){
		showToast(R.string.spaceIsLow_content);
		finish();
		return;
	}

	mContentResolver = mContext.getContentResolver();
	QuickCapture.setMInstance(this);
	getPreferredCameraId();
	openCamera();

	if (mOpenCameraFail) {
	    if (mVoiceRecognizer != null)
		    mVoiceRecognizer.playTTS(mContext.getString(R.string.tts_take_picture_failed));
	    else
		    Toast.makeText(mContext, R.string.cannot_connect_camera, Toast.LENGTH_LONG).show();
	    finish();
	    return;
	} else if (mCameraDisabled) {
	    if (mVoiceRecognizer != null)
		    mVoiceRecognizer.playTTS(mContext.getString(R.string.tts_take_picture_failed));
	    else
		    Toast.makeText(mContext, R.string.camera_disabled, Toast.LENGTH_LONG).show();
	    finish();
	    return;
	} else if (mCameraBusy) {
	    finish();
	    return;
	}
	initView();

	mCameraDevice.setErrorCallback(mErrorCallback);
	mErrorCallback.setHandler(mHandler);

	SurfaceHolder holder = mPreviewFrame.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mPreferences.setLocalId(mContext, mCameraId);
        CameraSettings.upgradeLocalPreferences(mPreferences.getLocal());

	mLocationManager = new LocationManager(mContext, null);
        mLocationManager.recordLocation(false);
	setPictureAnimation();
	mAnimationView_1.startAnimation(t1);
	mAnimationView_2.startAnimation(t2);
    }

    public void finish() {
	if (mPreviewFrameLayout != null) {
	    mPreviewFrameLayout.removeView(mAnimationView_1);
	    mPreviewFrameLayout.removeView(mAnimationView_2);
	}
	if (mWindowManager != null)
	    mWindowManager.removeView(mRootView);
	closeCamera();

	mContext = null;
	QuickCapture.setMInstance(null);
    }
    
    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
	if (CameraAppImpl.DEBUG)  Log.d(TAG, "surfaceChanged w = " + w + " h = " + h);
        // Make sure we have a surface in the holder before proceeding.
        if (holder.getSurface() == null) {
            return;
        }
        mSurfaceHolder = holder;

        if (mCameraState == PREVIEW_STOPPED) {
            startPreview();
            takePicture();
	    stopAudio();
	    startAudio(R.raw.camera_click);
        } else {
	    if (holder.isCreating()) {
	    	setPreviewDisplay(holder);
	    }
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceHolder = null;
    }

    private void closeCamera() {
        if (mCameraDevice != null) {
            if (CameraAppImpl.DEBUG) Log.d(TAG,"closeCamera------");
            CameraHolder.instance().release();
            mCameraDevice.setErrorCallback(null);
            mCameraDevice = null;
            mCameraState = PREVIEW_STOPPED;
        }
    }

    private void setPreviewDisplay(SurfaceHolder holder) {
        try {
            mCameraDevice.setPreviewDisplay(holder);
        } catch (Throwable ex) {
            closeCamera();
            throw new RuntimeException("setPreviewDisplay failed", ex);
        }
    }

    private void startPreview() {
        // If we're previewing already, stop the preview first (this will blank the screen).
        if (mCameraState != PREVIEW_STOPPED) stopPreview();

        setPreviewDisplay(mSurfaceHolder);

        setCameraParameters();
        // If the focus mode is continuous autofocus, call cancelAutoFocus to
        // resume it because it may have been paused by autoFocus call.
        if (Parameters.FOCUS_MODE_CONTINUOUS_PICTURE.equals(mParameters.getFocusMode())) {
            mCameraDevice.cancelAutoFocus();
        }

        try {
            if (CameraAppImpl.DEBUG) Log.d(TAG,"startPreview------");
	    mCameraDevice.startPreview();
        } catch (Throwable ex) {
            closeCamera();
            throw new RuntimeException("startPreview failed", ex);
        }
        mCameraState = IDLE;
    }

    private void stopPreview() {
        if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) {
            if (CameraAppImpl.DEBUG) Log.d(TAG, "stopPreview------");
            mCameraDevice.stopPreview();
        }
        mCameraState = PREVIEW_STOPPED;
    }

   private void setCameraParameters() {
        mParameters = mCameraDevice.getParameters();
	try {
		mPicWaterMark = Settings.System.getInt(mContext.getContentResolver(),"pic_watermark");
		mVideoWaterMark = Settings.System.getInt(mContext.getContentResolver(),"video_watermark");
	} catch(SettingNotFoundException e) {
		Log.e(TAG,"SettingNotFoundException");
	}

	mParameters.setPreviewSize(800, 600);
	mParameters.setPreviewFrameRate(100);

	mParameters.set("time_water_mark", CameraAppImpl.getWaterMarkMode(mPicWaterMark,mVideoWaterMark));
	Size size = getMaxSupportedPictureSize(mParameters.getSupportedPictureSizes());
	if (CameraAppImpl.DEBUG)
		Log.d(TAG,"maxSupportSize :: width = "+size.width+" height="+size.height);

	mParameters.setPictureSize(size.width, size.height);
	if (CameraAppImpl.NO_SCREEN_DEVICE) {
		mParameters.set("preview_mode", CameraAppImpl.NO_SCREEN);//ipu_direct
	}else {
		mParameters.set("preview_mode", CameraAppImpl.CAPTURE);
	}
	/* 设置使用CameraHal存储，此时默认使用CameraHalDisplay */
	mQuickCapture_HALStoreJpeg_fullpath = Storage.generate_QuickPicturefullname();
	mParameters.set("quickcapture-halstore-dir", mQuickCapture_HALStoreJpeg_fullpath);
	IS_USE_QuickCapture_HALStore = true;

	mCameraDevice.setParameters(mParameters);
    }

    public void initView() {
    	mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
    	mWindowParams = new WindowManager.LayoutParams();
    	mWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
    	mWindowParams.format = PixelFormat.RGBA_8888;
    	mWindowParams.flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
	    | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE;
    	mWindowParams.width = WindowManager.LayoutParams.MATCH_PARENT;
    	mWindowParams.height = WindowManager.LayoutParams.MATCH_PARENT;
    	mWindowParams.gravity = Gravity.CENTER;

	windowW = mWindowManager.getDefaultDisplay().getWidth();  
	windowH = mWindowManager.getDefaultDisplay().getHeight();
	if (CameraAppImpl.DEBUG) Log.d(TAG,"window w = "+windowW+" h = "+windowH);
	LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	mRootView = inflater.inflate(R.layout.preview_frame, null);
	mPreviewFrameLayout = (FrameLayout) mRootView.findViewById(R.id.frame);
        mPreviewFrame = (SurfaceView) mRootView.findViewById(R.id.camera_preview);
	mPreviewFrameLayout.setLayoutParams(new FrameLayout.LayoutParams(windowW, windowH));
	mPreviewFrame.setLayoutParams(new FrameLayout.LayoutParams(windowW, windowH));

	mAnimationView_1 = new ImageView(mContext);
    	mAnimationView_1.setBackgroundResource(R.color.quickAnimationbackground_1);
    	mAnimationView_1.setLayoutParams(new android.view.ViewGroup.LayoutParams(windowW/2, windowH));

	mAnimationView_2 = new ImageView(mContext);
    	mAnimationView_2.setBackgroundResource(R.color.quickAnimationbackground_2);
    	mAnimationView_2.setLayoutParams(new android.view.ViewGroup.LayoutParams(windowW/2, windowH));
    	mAnimationView_2.setX(windowW/2);

    	mPreviewFrameLayout.addView(mAnimationView_1);
    	mPreviewFrameLayout.addView(mAnimationView_2);

	mWindowManager.addView(mRootView, mWindowParams);
    }    
	private void setPictureAnimation() {

		t1 = new TranslateAnimation(Animation.ABSOLUTE, 0,
					    Animation.ABSOLUTE, -400,
					    Animation.ABSOLUTE, 0,
					    Animation.ABSOLUTE, 0);
		t2 = new TranslateAnimation(Animation.ABSOLUTE, 0, 
					    Animation.ABSOLUTE, 400,   
					    Animation.ABSOLUTE, 0, 
					    Animation.ABSOLUTE, 0);
		t1.setDuration(400);// 0.3s
		t2.setDuration(400);
		
		t1.setAnimationListener(new AnimationListener() {
			
			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onAnimationEnd(Animation arg0) {
				    mPreviewFrameLayout.removeView(mAnimationView_1);
				    mPreviewFrameLayout.removeView(mAnimationView_2);
			}
		});
	}

	private void notifyMediaScanner() {
		ContentResolver resolver = mContext.getContentResolver();
		// Insert into MediaStore.
		ContentValues values = new ContentValues(2);
		//values.put(ImageColumns.TITLE, title);
		//values.put(ImageColumns.DISPLAY_NAME, mQuickCapture_HALStoreJpeg_fullpath);
		//values.put(ImageColumns.DATE_TAKEN, date);
		values.put(ImageColumns.MIME_TYPE, "image/jpeg");
		//values.put(ImageColumns.ORIENTATION, orientation);
		values.put(ImageColumns.DATA, mQuickCapture_HALStoreJpeg_fullpath);
	      	//values.put(ImageColumns.SIZE, jpeg.length);
		//values.put(ImageColumns.WIDTH, width);
		//values.put(ImageColumns.HEIGHT, height);

		Uri uri = resolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
		if (uri == null) {
			Log.e(TAG, "Failed to write MediaStore");
		} else {
			if (!CameraAppImpl.NO_SCREEN_DEVICE)
			    BitmapManager.instance().getThumbnail(mContentResolver,ContentUris.parseId(uri), 
								  Images.Thumbnails.MINI_KIND, null, false);
		}
	}

	public synchronized static void setMInstance(QuickCapture mInstance) {
		QuickCapture.mInstance = mInstance;
	}

	public synchronized static QuickCapture getMInstance() {
		return mInstance;
	}

        private void showToast(int textResId) {
	    View v = View.inflate(mContext, R.layout.battery_toast, null);
	    TextView tv=(TextView) v.findViewById(R.id.toast);
	    tv.setText(textResId);
	    Toast toast = new Toast(mContext);
	    toast.setGravity(Gravity.CENTER, 0, 0);
	    toast.setDuration(0);
	    toast.setView(v);
	    toast.show();
	}

    private Size getMaxSupportedPictureSize(List<Size> list){
	if (CameraAppImpl.NO_SCREEN_DEVICE){
	    Log.i(TAG,"get list max size");
	    return list.get(list.size()-1);
	}
	int maxSupportWidth = list.get(0).width;
	int max = 0;
	int screenWidth = mContext.getResources().getDisplayMetrics().widthPixels;
	for (int i = 0 ; i < list.size(); i++){
	    if (list.get(i).width > maxSupportWidth && list.get(i).width % screenWidth == 0){
		maxSupportWidth = list.get(i).width;
		max = i;
	    }   
	    if (CameraAppImpl.DEBUG)	
		Log.d(TAG,"getSupportedPreviewSize :: width ="+list.get(i).width
		      +"   height="+list.get(i).height);
	}	
	return list.get(max);
    }

    synchronized private void startAudio(int resid){
	if (resid!=0) {
	    if (mPlayer == null)
		mPlayer = MediaPlayer.create(mContext, resid);
            if (mPlayer != null) {
            	mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            	mPlayer.setOnCompletionListener(new OnCompletionListener() {
			public void onCompletion(MediaPlayer mp) {
			    stopAudio();
			    if(CameraAppImpl.DEBUG) Log.d(TAG, "media play completion");
			}
		    });
            	mPlayer.setOnErrorListener(new OnErrorListener() {
			public boolean onError(MediaPlayer mp, int what, int extra) {
			    stopAudio();                    		
			    if(CameraAppImpl.DEBUG) Log.d(TAG, "media play error");
			    return true;
			}
		    });
                mPlayer.start();
		if(CameraAppImpl.DEBUG) Log.d(TAG,"media play start");
            }
        }
    }

    synchronized public void stopAudio() {
	if (mPlayer != null) {
	    try {
		if(CameraAppImpl.DEBUG) Log.d(TAG,"media play stop");
		mPlayer.stop();
		mPlayer.release();
	    } finally {
		mPlayer = null;
	    }
	}
    }

    private boolean checkStorage() {
        mPicturesRemaining = StorageSpaceUtil.getAvailableSpace();
        if (mPicturesRemaining > 0) {
            mPicturesRemaining = mPicturesRemaining / StorageSpaceUtil.PICTURE_SIZE;
        }
        return updateStorageHint();
    }

    private boolean updateStorageHint() {
        String noStorageText = null;
        if (mPicturesRemaining == StorageSpaceUtil.UNAVAILABLE) {
            noStorageText = mContext.getString(R.string.tts_no_storage);
        } else if (mPicturesRemaining == StorageSpaceUtil.PREPARING) {
            noStorageText = mContext.getString(R.string.tts_preparing_sd);
        } else if (mPicturesRemaining == StorageSpaceUtil.UNKNOWN_SIZE) {
            noStorageText = mContext.getString(R.string.tts_access_sd_fail);
        } else if (mPicturesRemaining < 0) {
            noStorageText = mContext.getString(R.string.tts_not_enough_space);
        }

        if (noStorageText != null) {
	    Log.e(TAG, "checkStorage " + noStorageText);
	    if (mVoiceRecognizer != null)
		    mVoiceRecognizer.playTTS(noStorageText);
            // if (mStorageHint == null) {
            //     mStorageHint = OnScreenHint.makeText(mContext, noStorageText);
            // } else {
            //     mStorageHint.setText(noStorageText);
            // }
            // mStorageHint.show();
	    return false;
        } else if (mStorageHint != null) {
            mStorageHint.cancel();
            mStorageHint = null;
        }
	return true;
    }

}